package com.elasticcloudservice.predict;

/**
 * @Author: Chy
 * @Description: 服务器
 * @Date: Created at 18:39 2018/4/12
 */

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Chy
 * @Description: 服务器类
 * @Date: 11:59 2018/3/10
 */
public class ElasticCloudServer implements Serializable {

    private static final long serialVersionUID = 8481880328925764659L;

    private String name; //服务器名称
    private int CPUs;  //当前剩余的CPU资源
    private int Mems; //当前剩余的MEM资源，单位为GB
    private int originCPU; //服务器初始CPU资源
    private int originMem; //服务器初始MEM资源
    private double useRate; // 资源使用率
    private double memDivCPU;  //当前剩余MEM/CPU
    private double originMemdivCPU; //服务器内存和CPU比例

    private List<String> vmList; //存储的虚拟机列表名称
    private List<VirtualMachine> virtualMachines; //存储的虚拟机

    /**
     * @Author: Chy
     * @Param:
     * @Description: 构造方法
     * @Date: 20:16 2018/4/16
     */
    public ElasticCloudServer(int ecsCPU, int ecsMems) {
        this.CPUs = ecsCPU;
        this.Mems = ecsMems;
        this.originCPU = ecsCPU;
        this.originMem = ecsMems;
        this.vmList = new ArrayList<String>();
        this.virtualMachines = new ArrayList<VirtualMachine>();
        this.originMemdivCPU = originMem * 1.0 / originCPU;
    }

    /**
     * @Author: Chy
     * @Param:
     * @Description: 构造方法
     * @Date: 20:16 2018/4/16
     */
    public ElasticCloudServer(String name, int ecsCPU, int ecsMems) {
        this.CPUs = ecsCPU;
        this.Mems = ecsMems;
        this.name = name;
        this.originCPU = ecsCPU;
        this.originMem = ecsMems;
        this.vmList = new ArrayList<String>();
        this.virtualMachines = new ArrayList<VirtualMachine>();
        this.originMemdivCPU = originMem * 1.0 / originCPU;
    }

    /**
     * @Author: Chy
     * @Param:
     * @Description: 添加虚拟机
     * @Date: 22:35 2018/4/12
     */
    public void add(VirtualMachine vm) {
        this.CPUs -= vm.getCPUs();
        this.Mems -= vm.getMems() / 1024;
        this.vmList.add(vm.getName());
        this.virtualMachines.add(vm);
        this.setUseRate(this.getUseRate());
    }

    /**
     * @Author: Chy
     * @Param:
     * @Description: 移除虚拟机
     * @Date: 22:35 2018/4/12
     */
    public void remove(VirtualMachine vm) {
        vmList.remove(vm.getName());
        virtualMachines.remove(vm);
        this.CPUs += vm.getCPUs();
        this.Mems += vm.getMems();
    }

    public List<String> getVmList() {
        return vmList;
    }

    public void setVmList(List<String> vmList) {
        this.vmList = vmList;
    }

    public int getCPUs() {
        return CPUs;
    }

    public void setCPUs(int CPUs) {
        this.CPUs = CPUs;
    }

    public int getMems() {
        return Mems;
    }

    public void setMems(int mems) {
        Mems = mems;
    }

    public List<VirtualMachine> getVirtualMachines() {
        return virtualMachines;
    }

    public void setVirtualMachines(List<VirtualMachine> virtualMachines) {
        this.virtualMachines = virtualMachines;
    }

    public int getOriginCPU() {
        return originCPU;
    }

    public void setOriginCPU(int originCPU) {
        this.originCPU = originCPU;
    }

    public int getOriginMem() {
        return originMem;
    }

    public void setOriginMem(int originMem) {
        this.originMem = originMem;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getUseRate() {
        return ((originCPU - CPUs) * 1.0 / originCPU + (originMem - Mems) * 1.0 / originMem) / 2;
    }

    public void setUseRate(double useRate) {
        this.useRate = useRate;
    }

    public double getMemDivCPU() {
        return memDivCPU;
    }

    public void setMemDivCPU(double memDivCPU) {
        this.memDivCPU = memDivCPU;
    }

    public double getOriginMemdivCPU() {
        return originMemdivCPU;
    }

    public void setOriginMemdivCPU(double originMemdivCPU) {
        this.originMemdivCPU = originMemdivCPU;
    }
}